package com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.base.Const;
import com.controller.base.BaseController;
import com.entity.File;
import com.entity.FileRel;
import com.entity.Trade;
import com.entity.User;
import com.entity.UserType;
import com.service.FileRelService;
import com.service.FileService;
import com.service.TradeRelService;
import com.service.TradeService;
import com.service.UserService;
import com.service.UserTypeService;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController<User>{

	@Autowired
	private UserService userService;
	@Autowired
	private UserTypeService userTypeService;
	@Autowired
	private TradeService tradeService;
	@Autowired
	private TradeRelService tradeRelService;
	@Autowired
	private FileService fileService;
	@Autowired
	private FileRelService fileRelService;
	
	//用户管理
	@RequestMapping("/index")
	public String index(){
		return "pages/user/user";
	}
	
	@RequestMapping("/info")
	@ResponseBody
	public AjaxRes findByPage(User user, Page<User> pages){
		AjaxRes res = new AjaxRes();
		pages = userService.findByPage(user, pages);
		res.setSucceed(pages, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/all")
	@ResponseBody
	public AjaxRes findAll(){
		AjaxRes res = new AjaxRes();
		List<User> users = userService.find(null);
		res.setSucceed(users, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/detail")
	@ResponseBody
	public AjaxRes find(User user){
		AjaxRes res = new AjaxRes();
		user = userService.find(user).get(0);
		res.setSucceed(user, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/pics")
	@ResponseBody
	public AjaxRes pisc(User user){
		AjaxRes res = new AjaxRes();
		user.setDetail(true);
		user.setFileRemark("normal");
		List<User> users = userService.find(user);
		if(users == null || users.size() == 0){
			res.setFailMsg(Const.DATA_FAIL);
		}else{
			res.setSucceed(users, Const.DATA_SUCCEED);
		}
		return res;
	}
	
	@RequestMapping("/portrait")
	@ResponseBody
	public AjaxRes portrait(User user){
		AjaxRes res = new AjaxRes();
		user.setDetail(true);
		user.setFileRemark("portrait");
		List<User> users = userService.find(user);
		if(users == null){
			res.setFailMsg(Const.DATA_FAIL);
		}else{
			res.setSucceed(users, Const.DATA_SUCCEED);
		}
		return res;
	}
	
	@RequestMapping("/del/pic")
	@ResponseBody
	public AjaxRes delpPis(FileRel rel){
		AjaxRes res = new AjaxRes();
		if(rel != null) fileRelService.delete(rel);
		res.setSucceedMsg(Const.DEL_SUCCEED);
		return res;
	}
	
	@RequestMapping("/types")
	@ResponseBody
	public AjaxRes findTypes(){
		AjaxRes res = new AjaxRes();
		List<UserType> types = userTypeService.find(null);
		res.setSucceed(types, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/other")
	@ResponseBody
	public AjaxRes findDetail(){
		AjaxRes res = new AjaxRes();
		List<Trade> trades = tradeService.find(null);
		List<UserType> types = userTypeService.find(null);
		Map<String,Object> maps = new HashMap<>();
		maps.put("trades", trades);
		maps.put("types", types);
		res.setSucceed(maps, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public AjaxRes add(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		//Map 转为JSON 再反序列化为bean和List
		String json = JSONArray.toJSONString(map.get("obj"));
		User user = JSON.parseArray(json, User.class).get(0);
		json = JSONArray.toJSONString(map.get("multi"));
		List<FileRel> rels = JSON.parseArray(json,FileRel.class);
		userService.insert(user,rels);
		res.setSucceedMsg(Const.SAVE_SUCCEED);
		return res;
	}

	@RequestMapping("/upd")
	@ResponseBody
	public AjaxRes update(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		String json = JSONArray.toJSONString(map.get("obj"));
		User user = JSON.parseArray(json, User.class).get(0);
		json = JSONArray.toJSONString(map.get("multi"));
		List<FileRel> rels = JSON.parseArray(json,FileRel.class);
		userService.update(user,rels);
		res.setSucceedMsg(Const.UPDATE_SUCCEED);
		return res;
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public AjaxRes delete(User user){
		AjaxRes res = new AjaxRes();
		userService.delete(user);
		res.setSucceed(Const.DEL_SUCCEED);
		return res;
	}
	
	@RequestMapping("/delall")
	@ResponseBody
	public AjaxRes delAllUser(@RequestBody List<User> users){
		AjaxRes res = new AjaxRes();
		userService.deleteBatch(users);
		//TODO SERVICE DELETE TABLE TRADEREL
		res.setSucceedMsg(Const.DEL_SUCCEED);
		return res;
	}
	
	//用户行业管理
	
	@RequestMapping("/trade")
	public String trade(){
		return "pages/user/trade";
	}
	
	@RequestMapping("/trades")
	@ResponseBody
	public AjaxRes findTrades(){
		AjaxRes res = new AjaxRes();
		List<Trade> trades = tradeService.find(null);
		res.setSucceed(trades, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/trade/info")
	@ResponseBody
	public AjaxRes findByPage(Trade trade, Page<Trade> pages){
		AjaxRes res = new AjaxRes();
		pages = tradeService.findByPage(trade, pages);
		res.setSucceed(pages, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/trade/detail")
	@ResponseBody
	public AjaxRes find(Trade trade){
		AjaxRes res = new AjaxRes();
		trade = tradeService.find(trade).get(0);
		res.setSucceed(trade, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/trade/add")
	@ResponseBody
	public AjaxRes addTrade(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		String json = JSONArray.toJSONString(map.get("obj"));
		Trade trade = JSON.parseArray(json, Trade.class).get(0);
		tradeService.insert(trade);
		res.setSucceedMsg(Const.SAVE_SUCCEED);
		return res;
	}
	
	@RequestMapping("/trade/upd")
	@ResponseBody
	public AjaxRes updateTrade(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		String json = JSONArray.toJSONString(map.get("obj"));
		Trade trade = JSON.parseArray(json, Trade.class).get(0);
		tradeService.update(trade);
		res.setSucceedMsg(Const.UPDATE_SUCCEED);
		return res;
	}
	
	@RequestMapping("/trade/del")
	@ResponseBody
	public AjaxRes delete(Trade trade){
		AjaxRes res = new AjaxRes();
		tradeService.delete(trade);
		res.setSucceed(Const.DEL_SUCCEED);
		return res;
	}
	
	@RequestMapping("/trade/delall")
	@ResponseBody
	public AjaxRes delAll(@RequestBody List<Trade> trades){
		AjaxRes res = new AjaxRes();
		tradeService.deleteBatch(trades);
		res.setSucceedMsg(Const.DEL_SUCCEED);
		return res;
	}
	
	//删除文件
	@RequestMapping("/file/del")
	public void delFile(File file){
		java.io.File relFile = new java.io.File(file.getFilePath() + file.getFileFile());
		if(relFile.exists()){
			if(relFile.delete()){
				fileService.delete(file);
			}
		}
	}
}
